package net.zjitc.controller;

import net.zjitc.pojo.Adminuser;
import net.zjitc.service.AdminService;
import net.zjitc.service.Impl.AdminServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by dell on 2017/11/18.
 */
public class Admin {
    AdminService service=new AdminServiceImpl();
    @URLMapping(url = "/AdminLogin")
    public String AdminLogin(HttpServletRequest request, HttpServletResponse response)
    {
        Adminuser adminuser=new Adminuser();
        List<Adminuser> list=service.findAllAdminuser();
        try {
            BeanUtils.populate(adminuser,request.getParameterMap());
            for(Adminuser a:list)
            {
                if(a.getUsername().equals(adminuser.getUsername())&&a.getPassword().equals(adminuser.getPassword()))
                {
                    HttpSession session=request.getSession();
                    session.setAttribute("admin",a);
                    return "admin/home.jsp";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @URLMapping(url = "/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session=request.getSession();
        session.removeAttribute("admin");
        return "admin/top.jsp";
    }

}
